<?php
class NotificationsController extends AppController {

	var $name = 'Notifications';
	var $uses = array('User','Notification','NotificationsUser');

    function beforeFilter () {
        parent::beforeFilter();

		// Aplico el Layout segun la Vista o si es AJAX
		if ($this->RequestHandler->isAjax()) {
			$this->layout = 'ajax';
		}else{
			// Cambio la Vista por la del Rol
			$this->layout = strtolower($this->Session->read('VistaActual'));
		}
    }

    function index() {

    }

    function getNotifications($current_url) {

		$arreglo_url_actual = explode("_", $current_url);

		// Armo la URL Actual con el formato /controlador/vista
		if(!isset($arreglo_url_actual[2])){
			$url_actual = "/".$arreglo_url_actual[1];
		}else{
			$url_actual = "/".$arreglo_url_actual[1]."/".$arreglo_url_actual[2];
		}

		// Averiguo cuales son las notificaciones a mostrar de la URL actual
		$arreglo_notificaciones = $this->Notification->query(
			"SELECT 
				Notification.id, Notification.name, Notification.url, Notification.texto, Notification.orden, Notification.id_elemento, Notification.position_tooltip
			FROM
				notifications AS Notification
			WHERE
				(Notification.url = '$url_actual' OR Notification.url = '*') AND Notification.id NOT IN 
				(
					SELECT 
						Notification.id
					FROM
						notifications AS Notification
						LEFT JOIN notifications_users AS notifications_users ON Notification.id = notifications_users.notification_id
					WHERE
						notifications_users.user_id = '".$this->Session->read('Auth.User.id')."'
				);
			"
		);

		$this->autoRender=false;
		echo json_encode($arreglo_notificaciones);
    }

	function actualizarusuario($id_notificacion) {

		// Actualizo el Usuario/Notificacion

		$this->NotificationsUser->create();

		// Seteo datos que no vienen del Formulario
		$this->NotificationsUser->set(array(
			'user_id' => $this->Session->read('Auth.User.id'),
			'notification_id' => $id_notificacion,
		));

		// Grabo cada Problema al Paciente
		$this->NotificationsUser->save();

		$this->autoRender=false;
	}
}
?>